package com.cliang.controller;

import com.cliang.tools.verifycode.VerifyCodeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 获取验证码
 */
@Controller
@RequestMapping(value = "/public/getVerifyCode")
public class GetVerifyCode extends HttpServlet{
    private static final long serialVersionUID = 3526200257155015149L;

    @RequestMapping
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode= VerifyCodeUtils.generateVerifyCode(4);

        //存入会话session
        HttpSession session=request.getSession(true);

        //删除以前的
        session.removeAttribute("verifyCode");
        session.setAttribute("verifyCode",verifyCode.toLowerCase());

        //生成图片
        int width=116,height=36;
        VerifyCodeUtils.outputImage(width,height,response.getOutputStream(),verifyCode);

    }
}
